package foo;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

public class Customer {

	private String _name;
	@SuppressWarnings("rawtypes")
	private Vector _rentals = new Vector();
	
	public Customer(String name) {
		_name = name;
	}
	
	@SuppressWarnings("unchecked")
	public void addRental(Rental arg) {
		_rentals.addElement(arg);
	}
	
	public String getName() {
		return _name;
	}
	
	@SuppressWarnings("rawtypes")
	public String statement() {
		double totalAmount = 0;
		int frequentRenterPoints = 0;
		Enumeration rentals = _rentals.elements();
		String result = "Rental Record for " + getName() + "\n";
		
		Map<Integer,Calculater> amountMap = new HashMap<Integer, Calculater>();
		amountMap.put(new Integer(Movie.REGULAR), new RegularCalculate());
		amountMap.put(new Integer(Movie.NEW_RELEASE), new NewReleaseCalculate());
		amountMap.put(new Integer(Movie.CHILDRENS), new ChildrensCalculate());
		
		while (rentals.hasMoreElements()) {
			double thisAmount = 0;
			Rental each = (Rental) rentals.nextElement();
			
			thisAmount = amountMap.get(new Integer(each.getMovie().getPriceCode())).calculate(thisAmount, each);
			//determine amounts for each line
//			switch (each.getMovie().getPriceCode()) {
//			case Movie.REGULAR:
//				thisAmount = getRegularAmount(thisAmount, each);
//				break;
//			case Movie.NEW_RELEASE:
//				thisAmount = getNewReleaseAmount(thisAmount, each);
//				break;
//			case Movie.CHILDRENS:
//				thisAmount = getChildrens(thisAmount, each);
//				break;
//			}
			
			// add frequent renter points
			frequentRenterPoints++;
			// add bonus for a two day new release rental
			if ((each.getMovie().getPriceCode() == Movie.NEW_RELEASE) && each.getDaysRented() > 1) 
				frequentRenterPoints++;
			
			// show figures for this rental
			result += "\t" + each.getMovie().getTitle() + "\t" + String.valueOf(thisAmount) + "\n";
			totalAmount += thisAmount;
		}
		
		// add footer lines
		result += "Amount owed is " + String.valueOf(totalAmount) + "\n";
		result += "You earned " + String.valueOf(frequentRenterPoints) + " frequent renter points";
		
		return result;
	}

	private interface Calculater {
		public double calculate(double amount, Rental rental);
	}
	private class ChildrensCalculate implements Calculater {

		@Override
		public double calculate(double amount, Rental rental) {
			amount += 1.5;
			if (rental.getDaysRented() > 3)
				amount += (rental.getDaysRented() - 3) * 1.5;
			return amount;
		}
		
	}
	private class NewReleaseCalculate implements Calculater {
		@Override
		public double calculate(double amount, Rental rental) {
			amount += rental.getDaysRented() * 3;
			return amount;
		}
	}
	private class RegularCalculate implements Calculater {
		@Override
		public double calculate(double amount, Rental rental) {
			amount += 2;
			if (rental.getDaysRented() > 2) 
				amount += (rental.getDaysRented() - 2) * 1.5;
			return amount;
		}
	}
	
	
//	private double getChildrens(double amount, Rental each) {
//		amount += 1.5;
//		if (each.getDaysRented() > 3)
//			amount += (each.getDaysRented() - 3) * 1.5;
//		return amount;
//	}
//
//	private double getNewReleaseAmount(double amount, Rental each) {
//		amount += each.getDaysRented() * 3;
//		return amount;
//	}
//
//	private double getRegularAmount(double amount, Rental each) {
//		amount += 2;
//		if (each.getDaysRented() > 2) 
//			amount += (each.getDaysRented() - 2) * 1.5;
//		return amount;
//	}
}
